﻿using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Types;
using QingZhouLinYe_Web.DbContext;
using QingZhouLinYe_Web.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.Web.WebSockets;

namespace QingZhouLinYe_Web.Ashxs
{
    /// <summary>
    /// test 的摘要说明
    /// </summary>
    public class test : IHttpHandler
    {

        private DateTime datetime = DateTime.Now;
        

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Location[] poits = new Location[10];
            Random ran = new Random();
            for(int i = 0; i < 10; i++)
            {
                poits[i] = new Location(BaseClass.RandomDouble(ran,118.33, 119.45), BaseClass.RandomDouble(ran,36.33, 36.45), BaseClass.RandomInt(ran,1,100));
            }
            Line line = new Line(poits);
            using (SqlSugarClient db = DbHelper.GetDb())
            {
                List<SheBei> sbs = new List<SheBei>();
                Random random = new Random();
                for (int i = 1; i < 10; i++)
                {
                    SheBei sb = new SheBei();
                    sb.IMEI = i.ToString();
                    sb.FangHuoDanWeiId = 2;
                    sb.LeiXingId = 1;
                    sb.Shape = new Location(BaseClass.RandomDouble(random, 118.33, 119.45), BaseClass.RandomDouble(random, 36.33, 36.45), BaseClass.RandomInt(random, 1, 200));
                    sb.ShiYongRen = "使用人" + i;
                    sb.LianXiFangShi = "联系方式" + i;
                    sb.LastOnLine = datetime;
                    sbs.Add(sb);
                    string shapestring = sb.Shape.ToSqlValue();
                }
                //string sql = "insert into SheBei (FangHuoDanWeiId,LeiXingId,IMEI,ShiYongRen,LianXiFangShi,Shape,LastOnLine) " +
                //        "values ("+sb.FangHuoDanWeiId+","+sb.LeiXingId+",'"+sb.IMEI+"','"+sb.ShiYongRen+"','"+sb.LianXiFangShi+"','"+sb.Shape+"','"+sb.LastOnLine+"')";
                //db.Ado.ExecuteCommand(sql);
                //    Debug.WriteLine(sb.Shape.ToSqlValue());

                //
                //context.Response.ContentType = "数据初始化成功！";
                //db.Insertable(qh).ExecuteReturnIdentity();
                db.Fastest<SheBei>().BulkCopy(sbs);
            }
            context.Response.Write("数据插入数据库成功\r\n" + line.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}